package com.yulian.util;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class RSAUtils {
	
	//公钥
    public  static String publicKeyStr;
    //私钥
    public static String privateKeyStr;
    
    public static String encrypt(String str, String publicKey) throws Exception{//key是16位str
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        return Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
    }
    
    public static String decrypt(String str, String privateKey) throws Exception{
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        return new String(cipher.doFinal(inputByte));
    }
    
    public static void main(String[] args) throws Exception { 
    	System.out.println("================密钥对构造完毕, 甲方将公钥公布给乙方, 开始进行加密数据的传输============="); 
    	String key = "yunli123";
    	publicKeyStr = "MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgF2MvrXskPtg1KlFonbgZuxFnU00qMZzzLniL6A/Yi98LkPHjcnTwfp4vWTas6FNHC93zfkhezvUgttHXknp/8oSFsAHO4z6C0+z7bjXX5mvPJLnloJWNoHRzWirpvuNOAKfXxOlRfD+S6lBIb3Slg1Xl4zFhKC+tFBpxpiiv6UPAgMBAAE=";
    	privateKeyStr = "MIICWgIBAAKBgF2MvrXskPtg1KlFonbgZuxFnU00qMZzzLniL6A/Yi98LkPHjcnTwfp4vWTas6FNHC93zfkhezvUgttHXknp/8oSFsAHO4z6C0+z7bjXX5mvPJLnloJWNoHRzWirpvuNOAKfXxOlRfD+S6lBIb3Slg1Xl4zFhKC+tFBpxpiiv6UPAgMBAAECgYAu01hSb4tSiHFqGTTtE/3wnhfSoV0pmKUNFi6Io7Fd22a8/pH2SEx8Nxt19BKd1p8ZE3rCDQRGOmUVpW+Ix9wEig9MQS4HeCLTZ63RXbT0yfYcrwnfVMtkWq+Jiq/AGDzNrRs3mGlRas1r1ukhygZI0KGbr5wp3uiVMAK4tQDZYQJBAKcjRF2UOQH7fM3ic2xF8LHxX/1kLZTyxBpNHhzxrGRrgKRzxQTZ4BHsoLwuimu6qM891opDhNpJjQ16ZSaTHzsCQQCPSZbRD61SmcUE8CL1vgXFXSCVDSp30vMkUecmOBzO+/jNrj5lur8jIfRp8NqpgIBqrTYc2fXw8lUKI31/6Fw9AkB1mTrhHQl/e5O0yUCtTgitK0h+4Z2wz59rYhduBh1mCr3Y+BwH7cTa18PApH6AjjV4m+n4Hsm7TUUR1qyVF69BAkAvpAoPDKVWF2ZYU/druXh1vJb+t9fs6yiIZOrz3ChH8aMxz8NTU5oFdl6rcrdhMCaCUYDyas3JBOmda1to0bsJAkAfXMZVeBTLTMoD2L+qzx3Mhv/x3fqq8FBxIj0KXgMohB/o66UlPld5yFMMlZ9TCD7as+xYAy1FYV0Kpfsztbsx";
    	try {
    		String enStr = encrypt(key, publicKeyStr);
    		System.out.println("yunli123 enStr: "+enStr); 
    		String deStr = decrypt(enStr, privateKeyStr);
    		System.out.println("yunli123 deStr: "+deStr);
    	}catch(Exception e) {
    		System.out.println("yunli123 exception: "+e.getMessage());
    	}
    	
    }


}
